package com.heima.common.aliyun;

import com.aliyun.imageaudit20191230.models.ScanTextResponseBody;
import com.aliyun.tea.TeaException;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "aliyun")
public class GreenTextScanFree {

    private String accessKeyId;
    private String secret;

    public Map greenTextScan(String content) throws Exception {
        if(content.isEmpty()){
            return null;
        }
        //配置参数对象
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(secret);
        //访问的域名
        config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
        //使用AK&SK初始化账号Client
        com.aliyun.imageaudit20191230.Client client = new com.aliyun.imageaudit20191230.Client(config);

        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels0 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("spam");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels1 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("politics");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels2 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("abuse");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels3 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("terrorism");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels4 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("porn");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels5 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("flood");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels6 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("contraband");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels7 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("ad");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestTasks tasks0 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestTasks()
                .setContent(content);
        com.aliyun.imageaudit20191230.models.ScanTextRequest scanTextRequest = new com.aliyun.imageaudit20191230.models.ScanTextRequest()
                .setTasks(java.util.Arrays.asList(
                        tasks0
                ))
                .setLabels(java.util.Arrays.asList(
                        labels0,
                        labels1,
                        labels2,
                        labels3,
                        labels4,
                        labels5,
                        labels6,
                        labels7
                ));
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            Map resultMap = new HashMap<>();
            // 复制代码运行请自行打印 API 的返回值
            com.aliyun.imageaudit20191230.models.ScanTextResponse scanTextResponse = client.scanTextWithOptions(scanTextRequest, runtime);
            if(200 == scanTextResponse.getStatusCode()){
                List<ScanTextResponseBody.ScanTextResponseBodyDataElements> elements = scanTextResponse.getBody().getData().getElements();
                if (!elements.isEmpty() && elements.size() > 0){
                    for (ScanTextResponseBody.ScanTextResponseBodyDataElements element : elements) {
                        List<ScanTextResponseBody.ScanTextResponseBodyDataElementsResults> results = element.getResults();
                        if(!results.isEmpty() && results.size()>0){
                            for (ScanTextResponseBody.ScanTextResponseBodyDataElementsResults result : results) {
                                String suggestion = result.getSuggestion();
                                String label = result.getLabel();
                                if(!suggestion.equals("pass")){
                                    resultMap.put("suggestion",suggestion);
                                    resultMap.put("label",label);
                                    return resultMap;
                                }
                            }
                        }
                    }
                    resultMap.put("suggestion","pass");
                    return resultMap;
                }else{
                    return null;
                }
            } else {
                return null;
            }
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return null;
    }
}
